package com.fast.cms.web.controller.article;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fast.base.db.page.PageView;
import com.fast.base.db.sql.AbstractSqlGenerator;
import com.fast.base.model.AnoSysLog;
import com.fast.base.util.LogUtils;
import com.fast.base.util.StringUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.ResponseEnum;
import com.fast.base.web.BaseController;
import com.fast.cms.model.article.NoticeInfo;
import com.fast.cms.model.system.Dictionary;
import com.fast.cms.service.article.NoticeInfoService;
import com.fast.cms.web.form.CheckNoticeInfoForm;
import com.fast.cms.web.form.NoticeInfoForm;
import com.fast.common.userCache.AdminCacheUtils;
import com.fast.common.userCache.UserRequestUtils;
import com.fast.user.model.admin.AdminUser;

/**
 * ClassName:NoticeInfoController <br/>
 * 用途:NoticeInfo信息控制处理 <br/>
 * 开发人员：lushuifa http://lushuifa.iteye.com/ 邮箱:lsflu@126.com
 * @version
 * @since JDK 1.6
 * @see Date: 2015年03月14日 23时37分17秒 <br/>
 */
@Controller
@RequestMapping("/noticeInfo")
public class NoticeInfoController extends BaseController{
	
	private Log log = LogFactory.getLog(NoticeInfoController.class);
	
	@Resource(name="noticeInfoService")
	private NoticeInfoService noticeInfoService;
	
	
	
	/**
	 * 审批资讯文章
	 * @param checkArticleForm
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/checkNoticeInfo.do")
	public ResponseData checkNoticeInfo(CheckNoticeInfoForm checkNoticeInfoForm){
		AdminUser curAdminUser = AdminCacheUtils.getLoginAdminUser();
		ResponseData j = new ResponseData();
		if(curAdminUser!=null){
			int checkerId = curAdminUser.getId();
			String checker = curAdminUser.getRealName();
			checkNoticeInfoForm.setCheckerId(checkerId);
			checkNoticeInfoForm.setChecker(checker);
			boolean actionFlag = this.noticeInfoService.checkNoticeInfo(checkNoticeInfoForm);
			if(actionFlag){
				//审核机关操作 审核通过时则应该扣除库存
				j.buildSuccessResponse();
			}else{
				j.setCode("01");
				j.setMsg("操作失败,请重试或者重新登录试试！");
			}
		}else{
			j.setCode("01");
			j.setMsg("请先登录再操作！");
		}
		return j;
	}
	
	/**
	 * 说明:限量查询通知列表 <br>
	 * 编写者:lushuifa
	 * 创建时间:2017年7月13日 下午11:07:16 </pre>
	 * @param 
	 * @return ResponseData 说明
	 */
	@ResponseBody
	@RequestMapping({"/getNoticeListForLimit.do"})
	public ResponseData getNoticeListForLimit(String noticeTypeId,int limitSize,String selectType){
		ResponseData responseData = new ResponseData();
		try{
			List<Map<String, Object>> articleList = this.noticeInfoService.getNoticeListForLimit(noticeTypeId, limitSize, selectType);
			responseData.setData(articleList);
		}catch(Exception e){
			e.printStackTrace();
			responseData.failue();
		}
 		return responseData;
	}	

	/**
	 * 编辑[NoticeInfoService]信息
	 * @param imageUrl
	 * @return
	 */
	@RequestMapping("/editNoticeInfo.do")
	@ResponseBody
	@AnoSysLog(actionName="编辑公告信息")
	public ResponseData editNoticeInfo(){
		ResponseData responseData = new ResponseData();
		NoticeInfo noticeInfo = null;
		try {
			int id = webUtil.getInt("id");
			if(id>0){
				noticeInfo = this.noticeInfoService.getById(NoticeInfo.class,id);
				noticeInfo = (NoticeInfo) webUtil.bindDataToBean(noticeInfo);
			}else{
				noticeInfo = new NoticeInfo();
				noticeInfo = (NoticeInfo) webUtil.bindDataToBean(noticeInfo);
				UserRequestUtils.bindCreaterInfo(noticeInfo);
			}
			
			String noticeTypeId = webUtil.getString("noticeTypeId");
			if(StringUtils.isNotEmpty(noticeTypeId)){
				Dictionary dic = this.noticeInfoService.queryForBean(Dictionary.class, " where uuid='"+noticeTypeId+"'");
				noticeInfo.setNoticeTypeId(noticeTypeId);
				noticeInfo.setNoticeType(dic.getDicname());
			}
			String showTypeId = webUtil.getString("showTypeId");
			if(StringUtils.isNotEmpty(showTypeId)){
				Dictionary dic = this.noticeInfoService.queryForBean(Dictionary.class, " where uuid='"+showTypeId+"'");
				noticeInfo.setShowTypeId(showTypeId);
				noticeInfo.setShowType(dic.getDicname());
			}
			UserRequestUtils.bindEditorInfo(noticeInfo);
			boolean actionFlag = this.noticeInfoService.saveOrUpdateNoticeInfo(noticeInfo);
			if(actionFlag){
				responseData.setMsg("编辑操作成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("操作失败,请核对后再操作!");
		}
		return responseData;
	}
	
	/**
	 * 删除[公告信息]信息到回收站
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delNoticeInfoByIdForHS.do")
	@AnoSysLog(actionName="删除公告到回收站")
	public ResponseData delNoticeInfoByIdForHS(int id) {
		ResponseData responseData = new ResponseData();
		try {
			String sql = "update "+AbstractSqlGenerator.getTableName(NoticeInfo.class)+" set delete_Flag=1 where id="+id;
			boolean actionFlag = noticeInfoService.modifyRow(sql)>0;
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.buildResult(ResponseEnum.DELETE_FAILUE);
		}
		return responseData;
	}
	
	/**
	 * 删除多个[公告信息]信息到回站
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelNoticeInfoForHS.do")
	@AnoSysLog(actionName="批量删除文章到回收站")
	public ResponseData batchDelNoticeInfoForHS(String ids){
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = this.noticeInfoService.batchDeleteByIdsForHS(NoticeInfo.class, ids);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	@ResponseBody
	@RequestMapping("/batchRebackData.do")
	@AnoSysLog(actionName="批量恢复已删除的数据")
	public ResponseData batchRebackData(String ids){
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = noticeInfoService.batchRebackDeleteData(NoticeInfo.class, ids);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.setMsg("你选中的数据恢复数据成功");
			}else{
				responseData.setMsg("你选中的数据恢复数据失败,请与管理员联系！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}

	/**
	 * 删除[公告信息]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delNoticeInfoById.do")
	@AnoSysLog(actionName="彻底删除某个公告")
	public ResponseData delNoticeInfoById(int id) {
		ResponseData responseData = new ResponseData();
		try {
			//删除对应的图片
			boolean actionFlag = noticeInfoService.deleteById(NoticeInfo.class, id);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.buildResult(ResponseEnum.DELETE_FAILUE);
		}
		return responseData;
	}
	
	/**
	 * 删除多个[公告信息]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelNoticeInfo.do")
	@AnoSysLog(actionName="批量删除公告")
	public ResponseData batchDelNoticeInfo(String ids){
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = noticeInfoService.deleteByIds(NoticeInfo.class, ids);
			responseData.setCode("00");
			responseData.setMsg("删除[公告信息]编号为["+ids+"]操作成功,共成功["+actionCount+"]");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("删除[公告信息]["+ids+"]操作失败");
		}
		return responseData;
	}
	
	/**
	 * 得到[公告信息]列表
	 * @return PageView<NoticeInfo>
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping(value="/getNoticeInfoList.do", method=RequestMethod.POST)
	public Map<String,Object> getNoticeInfoList(NoticeInfoForm noticeInfoForm) {
		Map<String,Object> gridMap = new HashMap<String,Object>();
		try{
			noticeInfoForm.setCurUserId(AdminCacheUtils.getLoginAdminUser().getId()+"");
			PageView<NoticeInfo> pageView = this.noticeInfoService.getNoticeInfoList(noticeInfoForm);
			gridMap.put("total", pageView.getTotalRecord());
			gridMap.put("rows", pageView.getRecords());
		}catch (Exception e) {
			LogUtils.error("getNoticeInfoList err:",e);
			e.printStackTrace();
		}
 		return gridMap;
	}
	
	/**
	 * 获取公告详情
	 */
	@ResponseBody
	@RequestMapping("/js/getNoticeInfoDetail.do")
	@AnoSysLog(actionName="获取资讯详情")
	public ResponseData getAjaxNoticeInfoDetail(int id) {
		LogUtils.info("getAjaxNoticeInfoDetail is comming!");
		ResponseData responseData = new ResponseData();
		try {
			NoticeInfo beanObj = this.noticeInfoService.getById(NoticeInfo.class,id);
			responseData.setData(beanObj);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtils.info("获取资讯详情getAjaxNoticeInfoDetail err",e);
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 查询详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/getNoticeInfoDetail.do")
	public ModelAndView getNoticeInfoDetail(int id,String actionType) {
		ModelAndView mv = new ModelAndView();
		NoticeInfo bean = noticeInfoService.queryForBeanById(NoticeInfo.class, id);
		try {
			mv.addObject("noticeInfo",bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(StringUtils.equals(actionType,"update")){
			mv.setViewName("/adminMain/noticeInfo/noticeInfo-add.jsp");
		}else{
			mv.setViewName("/manage/notice/noticeDetail.jsp");
		}
		return mv;
	}
	
}
